//
//  NewFromViewController.m
//  EMMApp
//
//  Created by Mac on 16/5/11.
//  Copyright © 2016年 YiXun. All rights reserved.
//

#import "NewFromViewController.h"
#import "NewCustomersCell.h"
#import "AttentionHuxingViewController.h"
#import "AddPayDepositApi.h"
#import "GetConsultantListApi.h"
#import "EditPayDepositApi.h"
#import "HousingViewController.h"
#import "AddSubscriptionCell.h"
#import "GetDictionaryListApi.h"


#import "NSString+Base64.h"
#import "NSData+Base64.h"
#import "QBImagePickerController.h"
#import "VGReturnPartPicViewCell.h"
#import "EMMImageView.h"
#import "MHPhotoBrowserController.h"
#import "GetDictionaryListByProjectIdApi.h"

@interface NewFromViewController ()<UITableViewDataSource,UITableViewDelegate,UITextFieldDelegate,UITextViewDelegate,QBImagePickerControllerDelegate,UIActionSheetDelegate,UINavigationControllerDelegate,UIImagePickerControllerDelegate>
{
    NSMutableDictionary *dic;//存对应的数据
    NSMutableArray *selectedArr;//二级列表是否展开状态
    NSMutableArray *titleDataArray;
    NSArray *dataArray;//数据源，显示每个cell的数据
    NSMutableDictionary *stateDic;//三级列表是否展开状态
    NSMutableArray *grouparr1;
    NSMutableArray *grouparr2;
    NSMutableArray *grouparr3;
//    NSInteger heightCell;
    NSInteger number;
    
    UITableView *_cellTableView;
    NSMutableDictionary *diccell;//存对应的数据
    NSMutableArray *selectedArrcell;//二级列表是否展开状态
    NSArray *dataArraycell;//数据源，显示每个cell的数据
    
    NSInteger ispayCellHeight;    //高度
}

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) UIButton *bcBtn;

@property (nonatomic, strong) UIButton *tjBtn;


@property (nonatomic, strong) UITextField *rcPricesTextField;       //总价

//第一屏
@property (nonatomic, strong) NSString *isPayDeposit;       //是否无房认筹
@property (nonatomic, strong) NSString *isPayDepositNumber;       //是否无房认筹Id

@property (nonatomic, strong) NSString *rcRoomName;//认筹名称
@property (nonatomic, strong) NSString *rcRoomId;//认筹房源id
@property (nonatomic, strong) NSString *calculateArea;//计价面积
@property (nonatomic, strong) NSString *discountPrice;//折前单价
@property (nonatomic, strong) NSString *discountTotalPrice;//折前总价

//第二屏
@property (nonatomic, strong) UITextField *khNameTextField;       //客户名字
@property (nonatomic, strong) NSString *khNameString;

@property (nonatomic, strong) NSString *khZJHLXString;        //客户证件号类型
@property (nonatomic, strong) NSString *khZJHNumber;            //id

@property (nonatomic, strong) UITextField *khZJHTextField;       //客户证件号
@property (nonatomic, strong) NSString *khZJHString;        //  客户证件号
@property (nonatomic, strong) UITextField *khYLHTextField;       //客户预留号码
@property (nonatomic, strong) NSString *khYLHString;        //  客户预留号码

@property (nonatomic, strong) UITextField *khYJDZTextField;       //客户邮寄地址
@property (nonatomic, strong) NSString *khYJDZString;        //  客户邮寄地址

@property (nonatomic, strong) UITextField *khYBTextField;       //客户邮编
@property (nonatomic, strong) NSString *khYBString;        //  客户邮编

@property (nonatomic, strong) UITextField *khRCHTextField;       //客户认筹号
@property (nonatomic, strong) NSString *khRCHString;        //  客户认筹号

@property (nonatomic, strong) UITextField *khRCJTextField;       //客户认筹金
@property (nonatomic, strong) NSString *khRCJString;        //  客户认筹金

@property (nonatomic, strong) UITextField *khBZTextField;       //客户备注
@property (nonatomic, strong) NSString *khBZString;        //  客户备注

@property (nonatomic, strong) NSString *timer;

@property (nonatomic, strong) NSString *agentId;        //经纪人;
@property (nonatomic, strong) NSString *agentName;        //经纪人;

@property (nonatomic, strong) GetConsultantList *getConsultantList;
@property (nonatomic, strong) GetCustomerDetail *getCustomerDetail;//客户详情

@property (nonatomic, strong) NSMutableArray *zyData;
@property (nonatomic, strong) NSMutableArray *zyDataArr;
@property (nonatomic, strong) NSMutableArray *zyDataArrId;


@property (nonatomic, strong) NSMutableArray *khxxDataArr;
@property (nonatomic, strong) UITextField *khxxTextField;
@property (nonatomic, strong) UITextField *khxxZJHTextField;

//房源
@property (nonatomic, strong) RoomList *roomList;

/**
 12、证件类型
 */
@property (nonatomic, strong) NSMutableArray *zjlxList;

//邮寄地址textview
@property (nonatomic, strong) UITextView *mailPostTV;

//邮寄地址textview高度
@property (nonatomic, assign) CGFloat mailPostTVHeight;

//备注textview
@property (nonatomic, strong) UITextView *remarkTV;

//备注textview高度
@property (nonatomic, assign) CGFloat remarkTVHeight;

/**
 选择图片数组
 */
@property(nonatomic, strong) NSMutableArray* picArray;

//上传图片数组
@property (nonatomic, strong) NSMutableArray *imageList;

@end

@implementation NewFromViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initWithShowUI];
    dic = [[NSMutableDictionary alloc] init];
    selectedArr = [[NSMutableArray alloc] initWithObjects:@"0",@"1",@"2", nil];
    dataArray = [[NSArray alloc] init];
    diccell = [[NSMutableDictionary alloc] init];
    selectedArrcell = [[NSMutableArray alloc] init];
    dataArraycell = [[NSArray alloc] init];
    //12、证件类型
    _zjlxList = [[NSMutableArray alloc] init];
    _zyData = [[NSMutableArray alloc] init];
    _zyDataArr = [[NSMutableArray alloc] init];
    _khxxDataArr = [[NSMutableArray alloc] init];
    _imageList = [[NSMutableArray alloc] init];
    
    _picArray = [[NSMutableArray alloc] init];
    NSMutableDictionary *dictIm = [[NSMutableDictionary alloc] init];
    [dictIm setObject:@"image" forKey:@"image"];
    [dictIm setObject:@"type" forKey:@"type"];
    [_picArray addObject:dictIm];
    
    [self postGetDictionaryListByProjectIdApi];

    [self postGetConsultantListApi];

//    [self creatNavigationController];

    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, EMM_WIDTH,EMM_HEIGHT-65-SafeAreaTopHeight) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.showsVerticalScrollIndicator = NO;
    //不要分割线
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_tableView];
    [self initDataSource];
    number = 1;
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    NSDate * date = [NSDate date];//当前时间
    _timer = [dateFormatter stringFromDate:date];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

#pragma mark - View
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
//    [IQKeyboardManager sharedManager].enable = YES;
}
#pragma mark - 查询客户详情api
- (void)postGetCustomerDetailApi {
    GetCustomerDetailApi *api = [[GetCustomerDetailApi alloc] initWithCustomerId:self.customerId];
    [api startWithCompletionBlockWithSuccess:^(YTKBaseRequest *request) {
        GetCustomerDetailApi *result = (GetCustomerDetailApi *)request;
        if (result.isSuccess) {
            GetCustomerDetail *get = result.resultData;
            _getCustomerDetail = get;
            _khNameString = _getCustomerDetail.customerName;
            _khZJHLXString = _getCustomerDetail.peDocumentType;
            for (int i = 0; i < _zjlxList.count; i++) {
                NSMutableDictionary *dict = [_zjlxList objectAtIndex:i];
                if ([_khZJHLXString isEqualToString:[dict objectForKey:@"dictionaryName"]]) {
                    _khZJHNumber = [dict objectForKey:@"dictionaryId"];
                }
            }
            _khZJHString = _getCustomerDetail.peDocumentNumber;
            _khYLHString = _getCustomerDetail.customerPhone;
            _khYJDZString = _getCustomerDetail.peContactAddress;//邮寄地址
            _khYBString = _getCustomerDetail.pePostcode;
            UITextView *mailPostTextView = [[UITextView alloc] initWithFrame:CGRectMake(100, 5, EMM_WIDTH - 100, 50)];
            mailPostTextView.font = [UIFont systemFontOfSize:15];
            mailPostTextView.text = _khYJDZString;
            _mailPostTVHeight = [self heightForString:mailPostTextView andWidth:EMM_WIDTH - 100];
        } else {
//            [self showToast:result.resultData.errorMsg];
            NSUserDefaults *userdf = [NSUserDefaults standardUserDefaults];
            NSString *projectCode = [userdf objectForKey:@"projectCode"];
            if ([NSString isStringEmpty2:projectCode] == NO) {
                [self showToast:result.resultData.errorMsg];
            }
            
            /*
             提示语：账号权限发生变动，请重新登录
             code随机18位唯一码：108E65843mYuwqb01X
             //YUplf0wyU5vPVssLSG 对应的异地登录退出登录
             */
            if ([result.resultData.errorCode isEqualToString:@"YUplf0wyU5vPVssLSG"] || [result.resultData.errorCode isEqualToString:@"108E65843mYuwqb01X"]) {
                NSUserDefaults *defs = [NSUserDefaults standardUserDefaults];
                NSDictionary *dictionary = [defs dictionaryRepresentation];
                for(NSString *key in [dictionary allKeys]){
                    if ([key isEqualToString:@"DeviceToken123"] ||
                        [key isEqualToString:@"loginText"] ||
                        [key isEqualToString:@"provinceCityDistrict"]) {
                    }else {
                        [defs removeObjectForKey:key];
                        [defs synchronize];
                    }
                }
                UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"温馨提示" message:result.resultData.errorMsg preferredStyle:UIAlertControllerStyleAlert];
                UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
                    [appInstance showLogin];
                }];
                [alertController addAction:cancelAction];
                [self presentViewController:alertController animated:YES completion:nil];
            }
        }
        [_tableView reloadData];
//        [self hideHud];
    } failure:^(YTKBaseRequest *request) {
        [self showToast:@"连接服务器失败，请检查网络！"];
//        [self hideHud];
    }];
}

#pragma  mark - 经纪人
- (void)postGetConsultantListApi {
    GetConsultantListApi *api = [[GetConsultantListApi alloc] initWithResourcesCode:nil searchType:nil];
    [api startWithCompletionBlockWithSuccess:^(YTKBaseRequest *request) {
        GetConsultantListApi *result = (GetConsultantListApi *)request;
        if (result.isSuccess) {
            GetConsultantList *get = result.resultData;
            _getConsultantList = get;
            NSArray *arr = [[NSArray alloc] init];
            for (ConsultantList *list in get.consultantList) {
                [_zyDataArr addObject:list.consultantUserName];
                [_zyData addObject:list];
                if ([_agentName isEqualToString:list.consultantUserName]) {
                    _agentId = list.consultantUserId;
                }
            }
            arr = [NSArray arrayWithObjects:_zyData, nil];
            NSUserDefaults *userdf = [NSUserDefaults standardUserDefaults];
            [userdf synchronize];
        } else {
            if ([result.resultData.errorCode isEqualToString:@"YUplf0wyU5vPVssLSG"] || [result.resultData.errorCode isEqualToString:@"108E65843mYuwqb01X"]) {
                NSUserDefaults *defs = [NSUserDefaults standardUserDefaults];
                NSDictionary *dictionary = [defs dictionaryRepresentation];
                for(NSString *key in [dictionary allKeys]){
                    if ([key isEqualToString:@"DeviceToken123"] ||
                        [key isEqualToString:@"loginText"] ||
                        [key isEqualToString:@"provinceCityDistrict"]) {
                    }else {
                        [defs removeObjectForKey:key];
                        [defs synchronize];
                    }
                }
                UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"温馨提示" message:result.resultData.errorMsg preferredStyle:UIAlertControllerStyleAlert];
                UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
                    [appInstance showLogin];
                }];
                [alertController addAction:cancelAction];
                [self presentViewController:alertController animated:YES completion:nil];
            }
        }
        [self creatNavigationController];
        [self initDataSource];

//        [self hideHud];
    } failure:^(YTKBaseRequest *request) {
        [self showToast:@"连接服务器失败，请检查网络！"];
//        [self hideHud];
    }];
}


#pragma mark - 导航
- (void)creatNavigationController {
    [self updateNavBar];
    if ([self.isFrom isEqualToString:@"0"]) {
        self.navigationItem.title = @"新增认筹";
        
    }else if([self.isFrom isEqualToString:@"1"]) {
        self.navigationItem.title = @"编辑认筹";
        [self creatEnditShowUI];
    }

    UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [backBtn setFrame:CGRectMake(0, 0, 54 ,44)];
    [backBtn addTarget:self
                action:@selector(fromToPreviousPage)
      forControlEvents:UIControlEventTouchUpInside];
    [backBtn setBackgroundColor:[UIColor clearColor]];
    [backBtn setImage:[UIImage imageNamed:@"ico_back"] forState:UIControlStateNormal];
    backBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    if (EMM_WIDTH <= 320) {
        backBtn.contentEdgeInsets = UIEdgeInsetsMake(0, -5, 0, 0);
    }else if (EMM_WIDTH > 375){
        backBtn.contentEdgeInsets = UIEdgeInsetsMake(0, -10, 0, 0);
    }else{
        backBtn.contentEdgeInsets = UIEdgeInsetsMake(0, -5, 0, 0);
    }    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backBtn];
    
    
}

- (void)creatEnditShowUI {
    [_khxxDataArr removeAllObjects];
    [_picArray removeAllObjects];
    _isPayDeposit = _getPayDepositDetail.isPayDeposit;
    if ([_getPayDepositDetail.isPayDeposit isEqualToString:@"0"]) {
        _isPayDeposit = @"否";
        grouparr1 = [[NSMutableArray alloc] initWithObjects:@"无房认筹",@"认筹房源",@"计价面积",@"折前单价",@"折前总价", nil];
        [dic setValue:grouparr1 forKey:@"0"];
    }else {
        _isPayDeposit = @"是";
        grouparr1 = [[NSMutableArray alloc] initWithObjects:@"无房认筹",nil];
        [dic setValue:grouparr1 forKey:@"0"];
    }
    _isPayDepositNumber = _getPayDepositDetail.isPayDeposit;
#pragma mark - 认筹房源名称
    //认筹房源名称
    if ([NSString isStringEmpty2:_rcRoomName] == NO) {
        
    }else {
        _rcRoomName = _getPayDepositDetail.payDepositHouses;
    }
    
    if ([NSString isStringEmpty2:_rcRoomId] == NO) {
        
    }else {
        if ([NSString isStringEmpty2:_getPayDepositDetail.roomId] == NO) {
            _rcRoomId = _getPayDepositDetail.roomId;
        }else {
            _rcRoomId = @"";
        }
    }
    if (_zjlxList.count > 0) {
        NSMutableArray *arr = [[NSMutableArray alloc] init];
        arr = _getPayDepositDetail.customerFamilyList;
        for (int i = 0; i < arr.count; i++) {
            CustomerFamilyList *list = [arr objectAtIndex:i];
            NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
            [dict setObject:list.memberName forKey:@"memberName"];
            NSString *documentType = @"";
            for (int i = 0; i < _zjlxList.count; i++) {
                NSMutableDictionary *dict = [_zjlxList objectAtIndex:i];
                if ([list.documentType isEqualToString:[dict objectForKey:@"dictionaryName"]]) {
                    documentType = [dict objectForKey:@"dictionaryId"];
                }
            }
            if ([NSString isStringEmpty2:documentType] == NO) {
                [dict setObject:documentType forKey:@"documentType"];
            }
            if ([
                NSString isStringEmpty2:list.documentNumber] == NO) {
                [dict setObject:list.documentNumber forKey:@"documentNumber"];
            }
            [_khxxDataArr addObject:dict];
        }
    }
    
    _calculateArea = _getPayDepositDetail.calculateArea;
    _discountPrice = _getPayDepositDetail.discountPrice;
    _discountTotalPrice = _getPayDepositDetail.discountTotalPrice;
    _khNameString = _getPayDepositDetail.customerName;
    _khZJHLXString = _getPayDepositDetail.documentType;
    for (int i = 0; i < _zjlxList.count; i++) {
        NSMutableDictionary *dict = [_zjlxList objectAtIndex:i];
        if ([_khZJHLXString isEqualToString:[dict objectForKey:@"dictionaryName"]]) {
            _khZJHNumber = [dict objectForKey:@"dictionaryId"];
        }
    }
    _khZJHString = _getPayDepositDetail.documentNumber;
    _khYLHString = _getPayDepositDetail.customerPhone;
    _khYJDZString = _getPayDepositDetail.mailPost;
    UITextView *mailPostTextView = [[UITextView alloc] initWithFrame:CGRectMake(100, 5, EMM_WIDTH - 100, 50)];
    mailPostTextView.font = [UIFont systemFontOfSize:15];
    mailPostTextView.text = _khYJDZString;
    _mailPostTVHeight = [self heightForString:mailPostTextView andWidth:EMM_WIDTH - 100];
    _khYBString = _getPayDepositDetail.postcode;
    _khRCHString = _getPayDepositDetail.payDepositCode;
    _khRCJString = _getPayDepositDetail.payDepositMoney;
    
    _agentName = _getPayDepositDetail.agentId;
    if (_agentName.length > 0) {
        for (int i = 0; i < _zyData.count; i++) {
            ConsultantList *list = [_zyData objectAtIndex:i];
            if ([_agentName isEqualToString:list.consultantUserName]) {
                _agentId = list.consultantUserId;
            }
        }
    }
    _timer = _getPayDepositDetail.payDepositDate;
    _khBZString = _getPayDepositDetail.remark;
    //计算备注textView的高度
    UITextView *remarkTextView = [[UITextView alloc] initWithFrame:CGRectMake(100, 5, EMM_WIDTH - 100, 50)];
    remarkTextView.font = [UIFont systemFontOfSize:15];
    remarkTextView.text = _khBZString;
    _remarkTVHeight = [self heightForString:remarkTextView andWidth:EMM_WIDTH - 100];
    
    for (ImageList *list in _getPayDepositDetail.imageList) {
        NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
        if ([NSString isStringEmpty2:list.smallUrl] == NO) {
            [dict setObject:list.smallUrl forKey:@"image"];
        }
        [dict setObject:@"2" forKey:@"type"];
        [_picArray addObject:dict];
    }
    
    NSMutableDictionary *dictIm = [[NSMutableDictionary alloc] init];
    [dictIm setObject:@"image" forKey:@"image"];
    [dictIm setObject:@"type" forKey:@"type"];
    [_picArray addObject:dictIm];
    
    [_tableView reloadData];
}


- (void)fromToPreviousPage {
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"是否要退出新增" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
//        [self NewCreatUpExit];
    }];
    UIAlertAction *otherAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
    }];
    [alertController addAction:cancelAction];
    [alertController addAction:otherAction];
//    [self presentViewController:alertController animated:YES completion:nil];
    [self.navigationController popViewControllerAnimated:YES];
    
    if ([self.isFrom isEqualToString:@"0"]) {
    }else if([self.isFrom isEqualToString:@"1"]) {
        if (_followUpViewBlock) {
            self.followUpViewBlock();
        }
    }
}

- (void)NewCreatUpExit {
    [self.navigationController popViewControllerAnimated:YES];
    
    if ([self.isFrom isEqualToString:@"0"]) {
    }else if([self.isFrom isEqualToString:@"1"]) {
        if (_followUpViewBlock) {
            self.followUpViewBlock();
        }
    }
}
#pragma mark - 初始化
- (void)initWithShowUI {
    _bcBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    _bcBtn.frame = CGRectMake(10, EMM_HEIGHT-65-SafeAreaTopHeight, EMM_WIDTH/2-20, 45);
    [_bcBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [_bcBtn setTitle:@"保存" forState:UIControlStateNormal];
    _bcBtn.layer.masksToBounds = YES;
    _bcBtn.layer.cornerRadius = 5;
    _bcBtn.backgroundColor = [UIColor colorFromHexCode:@"#f97242"];
    _bcBtn.titleLabel.font = [UIFont systemFontOfSize:16];
    [_bcBtn addTarget:self action:@selector(bcBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_bcBtn];
    
    _tjBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    _tjBtn.frame = CGRectMake(EMM_WIDTH/2+10, EMM_HEIGHT-65-SafeAreaTopHeight, EMM_WIDTH/2-20, 45);
    _tjBtn.backgroundColor = [UIColor colorFromHexCode:@"#c4302c"];
    [_tjBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [_tjBtn setTitle:@"提交" forState:UIControlStateNormal];
    _tjBtn.layer.masksToBounds = YES;
    _tjBtn.layer.cornerRadius = 5;
    _tjBtn.titleLabel.font = [UIFont systemFontOfSize:16];
    [_tjBtn addTarget:self action:@selector(tjBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_tjBtn];
}


-(void)initDataSource {
    titleDataArray = [[NSMutableArray alloc] initWithObjects:@"房源信息", @"客户信息",@"附件", nil];
    grouparr1 = [[NSMutableArray alloc] initWithObjects:@"无房认筹",@"认筹房源",@"计价面积",@"折前单价",@"折前总价", nil];
    grouparr2 = [[NSMutableArray alloc]initWithObjects:@"新增客户信息",@"客户姓名",@"证件类型",@"证件号码",@"手机号码",@"邮寄地址",@"邮  编", @"", @"认筹编号",@"认筹金",@"经纪人",@"认筹日期",@"备注", nil];
    grouparr3 = [[NSMutableArray alloc]initWithObjects:@"",nil];
    [dic setValue:grouparr1 forKey:@"0"];
    [dic setValue:grouparr2 forKey:@"1"];
    [dic setValue:grouparr3 forKey:@"2"];

}

#pragma mark --- tableViewDelegate
//返回几个表头
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return titleDataArray.count;
}

//每一个表头下返回几行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    NSString *string = [NSString stringWithFormat:@"%ld",section];
    if ([selectedArr containsObject:string]) {
        NSArray *array1 = dic[string];
        return array1.count;
    }
    return 0;
}

//设置表头的高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 50;
}

//Section Footer的高度
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
   
    return 5;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, EMM_WIDTH, 5)];
    view.backgroundColor = [UIColor clearColor];
    return view;
}

//设置view，将替代titleForHeaderInSection方法
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, EMM_WIDTH, 50)];
    view.backgroundColor = [UIColor whiteColor];
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 15, tableView.frame.size.width-20, 20)];
    titleLabel.text = [titleDataArray objectAtIndex:section];
    titleLabel.font = [UIFont systemFontOfSize:16];
    titleLabel.textColor = [UIColor colorFromHexCode:@"#1A1A1A"];
    [view addSubview:titleLabel];
    UIImageView *lineImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 49.5, EMM_WIDTH, 0.5)];
    lineImage.backgroundColor = [UIColor colorFromHexCode:@"#CCCCCC"];
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(0, 0, EMM_WIDTH, 50);
    button.tag = 100+section;
    [button addTarget:self action:@selector(doButton:) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:button];
    view.backgroundColor = [UIColor colorFromHexCode:@"#fff5e9"];
    UILabel *sLabel6 = [[UILabel alloc] initWithFrame:CGRectMake(0, 10, 4, 30)];
    sLabel6.backgroundColor = [UIColor colorFromHexCode:@"#ff7200"];
    [view addSubview:sLabel6];
    UIImageView *upImageView = [[UIImageView alloc] initWithFrame:CGRectMake(EMM_WIDTH-30, 20, 20, 10)];
    [view addSubview:upImageView];
    NSString *string = [NSString stringWithFormat:@"%ld",section];
    if ([selectedArr containsObject:string]) {
        upImageView.image = [UIImage imageNamed:@"sanjt_down"];
    }else {
        upImageView.image = [UIImage imageNamed:@"sanjt_up"];
    }
    return view;
}
//设置行高
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *indexStr = [NSString stringWithFormat:@"%ld",indexPath.section];
    if ([indexStr isEqualToString:@"0"]) {
        if (indexPath.row == 1) {
            NSInteger h = [IDTools textHeightFromTextString:_rcRoomName width:[UIScreen mainScreen].bounds.size.width-115 fontSize:15]+30;
            if (h <= 50) {
                h = 50;
            }
            return h;
        }
        return 50;
    }else if ([indexStr isEqualToString:@"1"]) {
        if (indexPath.row ==  5) {
            if (_mailPostTVHeight == 0) {
                _mailPostTVHeight = 34;
            }
            return _mailPostTVHeight + 16;
        }else if (indexPath.row == 7) {
            return _khxxDataArr.count * 200;
        }else if (indexPath.row == 12) {
            if (_remarkTVHeight == 0) {
                _remarkTVHeight = 34;
            }
            return _remarkTVHeight + 16;
        }
    }else if ([indexStr isEqualToString:@"2"]) {
        if (indexPath.row == 0) {
            NSInteger colx = _picArray.count%3;
            NSInteger coly = _picArray.count/3;
            
            if (colx != 0) {
                coly = coly + 1;
            }
            CGFloat appMargin=(EMM_WIDTH-3*100)/(3+1);
            
            return (100+appMargin)*coly;
        }
    }
    return 50;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    NSString *indexStr = [NSString stringWithFormat:@"%ld",indexPath.section];
    NSLog(@"%@",indexStr);
    static NSString *CellIdentifier = @"AddSubscriptionCell";
    AddSubscriptionCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"AddSubscriptionCell" owner:self options:nil] lastObject];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if ([selectedArr containsObject:indexStr]) {
        cell.titleLabel.text = dic[indexStr][indexPath.row];
    }
    if ([indexStr isEqualToString:@"0"]) {
        UIImageView *lineImage = [[UIImageView alloc] initWithFrame:CGRectMake(10, 49.5, EMM_WIDTH-20, 0.5)];
        lineImage.backgroundColor = [UIColor colorFromHexCode:@"#CCCCCC"];
        [cell addSubview:lineImage];
       
        if (indexPath.row == 0) {
            cell.biaoxinL.hidden = NO;
            cell.biaoxinL.text = @"*";
            cell.btn_qiaoJImageV.hidden = NO;
            if ([NSString isStringEmpty2:_isPayDeposit] == YES) {
                cell.subTitleLabel.text = @"未选择";
                cell.subTitleLabel.textColor = [UIColor colorFromHexCode:@"#C8C8C8"];
            }else {
                cell.subTitleLabel.text = _isPayDeposit;
                cell.subTitleLabel.textColor = [UIColor colorFromHexCode:@"#1A1A1A"];
            }
        }else if (indexPath.row == 1) {
            
            NSInteger h = [IDTools textHeightFromTextString:_rcRoomName width:[UIScreen mainScreen].bounds.size.width-115 fontSize:15]+30;
            if (h > 50) {
                lineImage.frame = CGRectMake(10, h - 0.5, EMM_WIDTH-20, 0.5);
            }
            cell.biaoxinL.hidden = NO;
            cell.biaoxinL.text = @"*";
            cell.btn_qiaoJImageV.hidden = NO;
            if ([NSString isStringEmpty2:_rcRoomName] == YES) {
                cell.subTitleLabel.text = @"未选择";
                cell.subTitleLabel.textColor = [UIColor colorFromHexCode:@"#C7C7CD"];
            }else {
                cell.subTitleLabel.text = _rcRoomName;
                cell.subTitleLabel.textColor = [UIColor blackColor];
            }
            
        }else if (indexPath.row == 2) {
            if ([NSString isStringEmpty2:_calculateArea] == YES) {
                cell.subtitleOneLable.text =@"m²";
            }else {
                cell.subtitleOneLable.text = [NSString stringWithFormat:@"%@m²",_calculateArea];
            }
           
        }else if (indexPath.row == 3) {
            
            if ([NSString isStringEmpty2:_discountPrice] == YES) {
                cell.subtitleOneLable.text =@"元";
            }else {
                cell.subtitleOneLable.text = [NSString stringWithFormat:@"%@元",_discountPrice];
            }
        }else if (indexPath.row == 4) {
            if ([NSString isStringEmpty2:_discountTotalPrice] == YES) {
                cell.subtitleOneLable.text =@"元";
            }else {
                cell.subtitleOneLable.text = [NSString stringWithFormat:@"%@元",_discountTotalPrice];
            }
        }
    }else if ([indexStr isEqualToString:@"1"]) {
        UIImageView *lineImage = [[UIImageView alloc] initWithFrame:CGRectMake(10, 49.5, EMM_WIDTH-20, 0.5)];
        lineImage.backgroundColor = [UIColor colorFromHexCode:@"#CCCCCC"];
        [cell addSubview:lineImage];
        
        if (indexPath.row == 0) {
            //新增客户信息
            cell.subTiltleTF.hidden = YES;
            cell.subTitleLabel.hidden = YES;
            UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(EMM_WIDTH-40, 10, 30, 30)];
            imageView.image = [UIImage imageNamed:@"rc_add"];
            [cell addSubview:imageView];
            cell.seleBtn.hidden = NO;
            [cell.seleBtn addTarget:self action:@selector(newKHCYBtnClick:) forControlEvents:UIControlEventTouchUpInside];
            
        }else if (indexPath.row == 1) {
            /**
             *  功能描述：
             *  1.修改姓名可以更改
             *  创建时间：16/5/11
             *
             *  更新时间：2017/02/17 下午
             *  更新人： wangguiping
             */
            cell.biaoxinL.hidden = NO;
            cell.biaoxinL.text = @"*";
            _khNameTextField = [[UITextField alloc] initWithFrame:CGRectMake(EMM_WIDTH-200-10, 10, 200, 30)];
            _khNameTextField.delegate = self;
            _khNameTextField.returnKeyType =UIReturnKeyDone;
            _khNameTextField.textAlignment = NSTextAlignmentRight;
            _khNameTextField.font = [UIFont systemFontOfSize:15];
            [cell addSubview:_khNameTextField];
            if ([NSString isStringEmpty2:_khNameString] == YES) {
                _khNameTextField.placeholder = @"请输入姓名";
            }else {
                _khNameTextField.text = _khNameString;
            }
//            if ([NSString isStringEmpty2:_khNameString] == YES) {
//                cell.subtitleOneLable.text = @"";
//                cell.subtitleOneLable.textColor = [UIColor colorFromHexCode:@"#C8C8C8"];
//            }else {
//                cell.subtitleOneLable.text = _khNameString;
//                cell.subtitleOneLable.textColor = [UIColor blackColor];
//            }
        }else if (indexPath.row == 2) {
            cell.btn_qiaoJImageV.hidden = NO;
            if ([NSString isStringEmpty2:_khZJHLXString] == YES) {
                cell.subTitleLabel.text = @"";
                cell.subTitleLabel.textColor = [UIColor colorFromHexCode:@"#C8C8C8"];
            }else {
                cell.subTitleLabel.text = _khZJHLXString;
                cell.subTitleLabel.textColor = [UIColor blackColor];
            }
        }else if (indexPath.row == 3) {
//            cell.biaoxinL.hidden = NO;
          
            _khZJHTextField = [[UITextField alloc] initWithFrame:CGRectMake(EMM_WIDTH-200-10, 10, 200, 30)];
            _khZJHTextField.delegate = self;
            _khZJHTextField.returnKeyType =UIReturnKeyDone;
            _khZJHTextField.textAlignment = NSTextAlignmentRight;
            _khZJHTextField.font = [UIFont systemFontOfSize:15];
            [cell addSubview:_khZJHTextField];
            if ([NSString isStringEmpty2:_khZJHString] == YES) {
                _khZJHTextField.placeholder = @"请输入证件号";
            }else {
                _khZJHTextField.text = _khZJHString;
            }
//            if ([NSString isStringEmpty2:_khZJHString] == YES) {
//                cell.customLabel.text = @"";
//            }else {
//                cell.customLabel.text = _khZJHString;
//            }
        }else if (indexPath.row == 4) {
            cell.biaoxinL.hidden = NO;
            cell.biaoxinL.text = @"*";
            _khYLHTextField = [[UITextField alloc] initWithFrame:CGRectMake(EMM_WIDTH-200-10, 10, 200, 30)];
            _khYLHTextField.delegate = self;
            _khYLHTextField.returnKeyType =UIReturnKeyDone;
            _khYLHTextField.textAlignment = NSTextAlignmentRight;
            _khYLHTextField.font = [UIFont systemFontOfSize:15];
//            [cell addSubview:_khYLHTextField];
//            if ([NSString isStringEmpty2:_khYLHString] == YES) {
//                _khYLHTextField.placeholder = @"请输入手机号";
//            }else {
//                _khYLHTextField.text = _khYLHString;
//            }
            if ([NSString isStringEmpty2:_khYLHString] == YES) {
                cell.subtitleOneLable.text = @"";
                cell.subtitleOneLable.textColor = [UIColor colorFromHexCode:@"#C8C8C8"];
            }else {
                cell.subtitleOneLable.text = _khYLHString;
                cell.subtitleOneLable.textColor = [UIColor blackColor];
            }
        }else if (indexPath.row == 5) {
//            cell.biaoxinL.hidden = NO;
/*            NSDictionary *attributes = @{NSFontAttributeName:[UIFont systemFontOfSize:15],};
            CGSize textSize = [@"邮寄地址" boundingRectWithSize:CGSizeMake(100, 100) options:NSStringDrawingTruncatesLastVisibleLine attributes:attributes context:nil].size;
            _khYJDZTextField = [[UITextField alloc] initWithFrame:CGRectMake(20+textSize.width, 10, EMM_WIDTH-20-10-textSize.width, 30)];
//            _khYJDZTextField = [[UITextField alloc] initWithFrame:CGRectMake(EMM_WIDTH-250-10, 10, 250, 30)];
            _khYJDZTextField.delegate = self;
            _khYJDZTextField.returnKeyType =UIReturnKeyDone;
            _khYJDZTextField.textAlignment = NSTextAlignmentRight;
            [cell addSubview:_khYJDZTextField];
            _khYJDZTextField.font = [UIFont systemFontOfSize:15];
            if ([NSString isStringEmpty2:_khYJDZString] == YES) {
                _khYJDZTextField.placeholder = @"请输邮寄地址";
            }else {
                _khYJDZTextField.text = _khYJDZString;
            }
//            if ([NSString isStringEmpty2:_khYJDZString] == YES) {
//                cell.customLabel.text = @"";
//            }else {
//                cell.customLabel.text = _khYJDZString;
//            }*/
            cell.subOneTF.hidden = YES;
            _mailPostTV = [[UITextView alloc] initWithFrame:CGRectMake(60 + 30, 5, EMM_WIDTH-100, _mailPostTVHeight + 6)];
            _mailPostTV.delegate = self;
            _mailPostTV.returnKeyType =UIReturnKeyDone;
            _mailPostTV.textAlignment = NSTextAlignmentRight;
            [cell addSubview:_mailPostTV];
            _mailPostTV.font = [UIFont systemFontOfSize:15];
            if ([NSString isStringEmpty2:_khYJDZString] == NO) {
                _mailPostTV.text = _khYJDZString;
                _mailPostTV.textColor = [UIColor colorFromHexCode:@"#1A1A1A"];
            }else {
                _mailPostTV.text = @"请输入邮寄地址";
                _mailPostTV.textColor = [UIColor colorFromHexCode:@"#CCCCCC"];
            }
            lineImage.hidden = YES;
            UIImageView *lineImage1 = [[UIImageView alloc] init];
            lineImage1.frame = CGRectMake(10, _mailPostTVHeight + 16 - 0.5, EMM_WIDTH - 20, 0.5);
            lineImage1.backgroundColor = [UIColor colorFromHexCode:@"#CCCCCC"];
            [cell addSubview:lineImage1];
        }else if (indexPath.row == 6) {
//            cell.biaoxinL.hidden = NO;
            
            _khYBTextField = [[UITextField alloc] initWithFrame:CGRectMake(EMM_WIDTH-200-10, 10, 200, 30)];
            _khYBTextField.delegate = self;
            _khYBTextField.returnKeyType =UIReturnKeyDone;
            _khYBTextField.textAlignment = NSTextAlignmentRight;
            _khYBTextField.font = [UIFont systemFontOfSize:15];
            [cell addSubview:_khYBTextField];
            if ([NSString isStringEmpty2:_khYBString] == YES) {
                _khYBTextField.placeholder = @"请输入邮编";
            }else {
                _khYBTextField.text = _khYBString;
            }
//            if ([NSString isStringEmpty2:_khYBString] == YES) {
//                cell.customLabel.text = @"";
//            }else {
//                cell.customLabel.text = _khYBString;
//            }
        }else if (indexPath.row == 7) {
            lineImage.hidden = YES;
            cell.subTiltleTF.hidden = YES;
            cell.subTitleLabel.hidden = YES;
            for (int i = 0; i < _khxxDataArr.count; i++) {
                NSMutableDictionary *dict = [_khxxDataArr objectAtIndex:i];
                NSString *nameString = [dict objectForKey:@"memberName"];
                NSString *documentType = [dict objectForKey:@"documentType"];
                NSString *documentTypeName = nil;
                NSString *documentNumber = [dict objectForKey:@"documentNumber"];
                if ([NSString isStringEmpty2:documentType] == NO) {
                    
                    for (int i = 0; i < _zjlxList.count; i++) {
                        NSMutableDictionary *dict = [_zjlxList objectAtIndex:i];
                        if ([documentType isEqualToString:[dict objectForKey:@"dictionaryId"]]) {
                            documentTypeName = [dict objectForKey:@"dictionaryName"];
                        }
                    }
                }
                UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, i*200, EMM_WIDTH, 200)];
                UIView *v1 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, EMM_WIDTH, 50)];
                v1.backgroundColor = [UIColor colorFromHexCode:@"#FFF5E9"];
                [view addSubview:v1];
                UILabel *sLabel6 = [[UILabel alloc] initWithFrame:CGRectMake(0, 10, 2, 30)];
                sLabel6.backgroundColor = [UIColor colorFromHexCode:@"#ff7200"];
                [view addSubview:sLabel6];
                
                UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
                button.frame = CGRectMake(EMM_WIDTH-40, 10, 30, 30);
                [button setImage:[UIImage imageNamed:@"rc_dlet"] forState:UIControlStateNormal];
                button.tag = 100 + i;
                [button addTarget:self action:@selector(dletjtcyBtnClick:) forControlEvents:UIControlEventTouchUpInside];
                [view addSubview:button];
                [cell addSubview:view];
                
                UILabel *titllName = [[UILabel alloc] initWithFrame:CGRectMake(10, 15, 100, 20)];
                titllName.font = [UIFont systemFontOfSize:15];
                titllName.text = [NSString stringWithFormat:@"家庭成员%d", i+1];
                [view addSubview:titllName];
                
                UILabel *titllName1 = [[UILabel alloc] initWithFrame:CGRectMake(10, 65, 100, 20)];
                titllName1.font = [UIFont systemFontOfSize:15];
                titllName1.text = @"客户姓名";
                titllName1.textColor = [UIColor colorFromHexCode:@"#5A5A5A"];
                [view addSubview:titllName1];
                
                UITextField *nameTextField = [[UITextField alloc] initWithFrame:CGRectMake(110, 60, EMM_WIDTH-120, 30)];
                nameTextField.font = [UIFont systemFontOfSize:15];
                nameTextField.textAlignment = NSTextAlignmentRight;
                nameTextField.delegate = self;
                nameTextField.tag = 100 + i;
                [nameTextField addTarget:self action:@selector(khNameTextFieldClick:) forControlEvents:UIControlEventEditingChanged];
                if ([NSString isStringEmpty2:nameString] == NO) {
                    nameTextField.text = nameString;
                    nameTextField.textColor = [UIColor colorFromHexCode:@"#1A1A1A"];

                }else {
                    nameTextField.placeholder = @"请输入";
                }
                [view addSubview:nameTextField];
                
                UIImageView *lineImage1 = [[UIImageView alloc] initWithFrame:CGRectMake(10, 99, EMM_WIDTH-20, 1)];
                lineImage1.backgroundColor = [UIColor colorFromHexCode:@"#e4e4e4"];
                [view addSubview:lineImage1];
                
                UILabel *titllName2 = [[UILabel alloc] initWithFrame:CGRectMake(10, 115, 100, 20)];
                titllName2.font = [UIFont systemFontOfSize:15];
                titllName2.text = @"证件类型";
                titllName2.textColor = [UIColor colorFromHexCode:@"#5A5A5A"];
                [view addSubview:titllName2];
                
                UILabel *tNameLabel2 = [[UILabel alloc] initWithFrame:CGRectMake(110, 115, EMM_WIDTH-132, 20)];
                tNameLabel2.font = [UIFont systemFontOfSize:15];
                tNameLabel2.textAlignment = NSTextAlignmentRight;
                [view addSubview:tNameLabel2];
                
                if ([NSString isStringEmpty2:documentTypeName] == NO) {
                    tNameLabel2.text = documentTypeName;
                    tNameLabel2.textColor = [UIColor colorFromHexCode:@"#1A1A1A"];
                }else {
                    tNameLabel2.text = @"未选择";
                    tNameLabel2.textColor = [UIColor colorFromHexCode:@"#C7C7CD"];
                }
                
                UIImageView *lineImage2 = [[UIImageView alloc] initWithFrame:CGRectMake(10, 149.5, EMM_WIDTH-20, 0.5)];
                lineImage2.backgroundColor = [UIColor colorFromHexCode:@"#CCCCCC"];
                [view addSubview:lineImage2];
                
                UIImageView *im = [[UIImageView alloc] initWithFrame:CGRectMake(EMM_WIDTH-10-12, 100+(50-16)/2, 12, 16)];
                im.image = [UIImage imageNamed:@"btn_qiaoj"];
                [view addSubview:im];
                
                UIButton *jjrbtn = [UIButton buttonWithType:UIButtonTypeSystem];
                jjrbtn.frame =CGRectMake(0, 115, EMM_WIDTH, 50);
                [jjrbtn addTarget:self action:@selector(zjleiBtnClick:) forControlEvents:UIControlEventTouchUpInside];
                jjrbtn.tag = 100 + i;
                [view addSubview:jjrbtn];
                
                UILabel *titllName3 = [[UILabel alloc] initWithFrame:CGRectMake(10, 165, 100, 20)];
                titllName3.font = [UIFont systemFontOfSize:15];
                titllName3.text = @"证件号码";
                titllName3.textColor = [UIColor colorFromHexCode:@"#5A5A5A"];
                [view addSubview:titllName3];
                
                UITextField *zjhmTextField = [[UITextField alloc] initWithFrame:CGRectMake(110, 160, EMM_WIDTH-120, 30)];
                zjhmTextField.font = [UIFont systemFontOfSize:15];
                zjhmTextField.textAlignment = NSTextAlignmentRight;
                zjhmTextField.delegate = self;
                zjhmTextField.tag = 100 + i;
                [zjhmTextField addTarget:self action:@selector(zjhmTextFieldFieldClick:) forControlEvents:UIControlEventEditingChanged];
                
                if ([NSString isStringEmpty2:documentNumber] == NO) {
                    zjhmTextField.text = documentNumber;
                    zjhmTextField.textColor = [UIColor colorFromHexCode:@"#1A1A1A"];

                }else {
                    zjhmTextField.placeholder = @"请输入";
                }
                [view addSubview:zjhmTextField];
                
                UIImageView *lineImage3 = [[UIImageView alloc] initWithFrame:CGRectMake(10, 199, EMM_WIDTH-20, 1)];
                lineImage3.backgroundColor = [UIColor colorFromHexCode:@"#e4e4e4"];
                [view addSubview:lineImage3];
            }
            
        }else if (indexPath.row == 8) {
//            cell.bitianLabel.hidden = NO;
          //认筹编号
            _khRCHTextField = [[UITextField alloc] initWithFrame:CGRectMake(EMM_WIDTH-200-10, 10, 200, 30)];
            _khRCHTextField.delegate = self;
            _khRCHTextField.returnKeyType =UIReturnKeyDone;
            _khRCHTextField.textAlignment = NSTextAlignmentRight;
            [cell addSubview:_khRCHTextField];
            _khRCHTextField.font = [UIFont systemFontOfSize:15];
            if ([NSString isStringEmpty2:_khRCHString] == YES) {
                _khRCHTextField.placeholder = @"请输入认筹编号";
            }else {
                _khRCHTextField.text = _khRCHString;
            }
        }else if (indexPath.row == 9) {
            cell.biaoxinL.hidden = NO;
            cell.biaoxinL.text = @"*";
            //认筹金
           
            UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(EMM_WIDTH-30-10, 10, 30, 30)];
            label.textAlignment = NSTextAlignmentRight;
            label.text = @"元";
            label.font = [UIFont systemFontOfSize:15];
            [cell addSubview:label];
            
            _khRCJTextField = [[UITextField alloc] initWithFrame:CGRectMake(EMM_WIDTH-200-30, 10, 200, 30)];
            _khRCJTextField.delegate = self;
            _khRCJTextField.returnKeyType =UIReturnKeyDone;
            _khRCJTextField.textAlignment = NSTextAlignmentRight;
            _khRCJTextField.font = [UIFont systemFontOfSize:15];
            [cell addSubview:_khRCJTextField];
            _khRCJTextField.keyboardType = UIKeyboardTypeDecimalPad;
            if ([NSString isStringEmpty2:_khRCJString] == YES) {
                _khRCJTextField.placeholder = @"请输入认筹金";
            }else {
                _khRCJTextField.text = _khRCJString;
            }
        }else if (indexPath.row == 10) {
            //经纪人
            cell.biaoxinL.hidden = NO;
            cell.biaoxinL.text = @"*";
            cell.btn_qiaoJImageV.hidden = NO;
            if ([NSString isStringEmpty2:_agentName] == YES) {
                NSUserDefaults *userdf = [NSUserDefaults standardUserDefaults];
                NSString *userName = [userdf objectForKey:@"userName"];
                NSString *userId = [userdf objectForKey:@"userNameId"];
                _agentName = userName;
                _agentId = userId;
                cell.subTitleLabel.text = _agentName;
            }else {
                cell.subTitleLabel.text = _agentName;
            }
        }else if (indexPath.row == 11) {
            //认筹时间
            cell.biaoxinL.hidden = NO;
            cell.biaoxinL.text = @"*";
            cell.btn_qiaoJImageV.hidden = NO;
            if ([NSString isStringEmpty2:_timer] == YES) {
               cell.subTitleLabel.text = @"未选择";
                cell.subTitleLabel.textColor = [UIColor colorFromHexCode:@"#C8C8C8"];
            }else {
                cell.subTitleLabel.text = _timer;
                cell.subTitleLabel.textColor = [UIColor blackColor];
            }
        }else if (indexPath.row == 12) {
            //备注
/*            cell.biaoxinL.hidden = YES;
            _khBZTextField = [[UITextField alloc] initWithFrame:CGRectMake(EMM_WIDTH-200-10, 15, 200, 20)];
            _khBZTextField.delegate = self;
            _khBZTextField.returnKeyType =UIReturnKeyDone;
            _khBZTextField.textAlignment = NSTextAlignmentRight;
            [cell addSubview:_khBZTextField];
            _khBZTextField.font = [UIFont systemFontOfSize:15];
            if ([NSString isStringEmpty2:_khBZString] == YES) {
                _khBZTextField.placeholder = @"请输入备注";
            }else {
                _khBZTextField.text = _khBZString;
            }*/
            cell.subOneTF.hidden = YES;
            _remarkTV = [[UITextView alloc] initWithFrame:CGRectMake(60 + 30, 5, EMM_WIDTH-100, _remarkTVHeight + 6)];
            _remarkTV.delegate = self;
            _remarkTV.returnKeyType =UIReturnKeyDone;
            _remarkTV.textAlignment = NSTextAlignmentRight;
            [cell addSubview:_remarkTV];
            _remarkTV.font = [UIFont systemFontOfSize:15];
            if ([NSString isStringEmpty2:_khBZString] == NO) {
                _remarkTV.text = _khBZString;
                _remarkTV.textColor = [UIColor colorFromHexCode:@"#1A1A1A"];
            }else {
                _remarkTV.text = @"备注";
                _remarkTV.textColor = [UIColor colorFromHexCode:@"#CCCCCC"];
            }
            lineImage.hidden = YES;
            UIImageView *lineImage1 = [[UIImageView alloc] init];
            lineImage1.frame = CGRectMake(10, _remarkTVHeight + 16 - 0.5, EMM_WIDTH - 20, 0.5);
            lineImage1.backgroundColor = [UIColor colorFromHexCode:@"#CCCCCC"];
            [cell addSubview:lineImage1];
        }
    } else if ([indexStr isEqualToString:@"2"]) {
        if (indexPath.row == 0) {
            cell.titleLabel.hidden = YES;
            int columnCount=3;
            //没个格子的宽度和高度
            CGFloat appW=100;
            CGFloat appH=100;
            //计算间隙
            CGFloat appMargin=(EMM_WIDTH-columnCount*appW)/(columnCount+1);
            for (int i=0; i<_picArray.count; i++) {
                UIView *vi=[[UIView alloc] init];
                vi.backgroundColor=[UIColor whiteColor];
                
                //计算列号和行号
                int colX=i%columnCount;
                int rowY=i/columnCount;
                //计算坐标
                CGFloat appX=appMargin+colX*(appW+appMargin);
                CGFloat appY=rowY*(appH+appMargin);
                NSInteger gdH = 0;
                if (i <= 2) {
                    gdH = 10;
                }else {
                    gdH = 0;
                }
                vi.frame=CGRectMake(appX, appY+gdH, appW, appH);
                [cell addSubview:vi];
                
                UIImageView *im = [[UIImageView alloc] initWithFrame:CGRectMake(5, 5, 90, 90)];
                [vi addSubview:im];
                NSMutableDictionary *dict = [_picArray objectAtIndex:i];
                NSString *type = [dict objectForKey:@"type"];
                
                if ([type isEqualToString:@"type"]) {
                    UIButton *buton = [UIButton buttonWithType:UIButtonTypeSystem];
                    buton.frame = CGRectMake(0, 0, 100, 100);
                    buton.backgroundColor = [UIColor whiteColor];
                    [buton addTarget:self action:@selector(butonClick:) forControlEvents:UIControlEventTouchUpInside];
                    [vi addSubview:buton];
                    
                    UIImageView *xuxianImageV = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
                    xuxianImageV.image = [UIImage imageNamed:@"xuxian"];
                    [vi addSubview:xuxianImageV];
                    
                    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(40, 25, 20, 20)];
                    imageView.image = [UIImage imageNamed:@"btn_more"];
                    [vi addSubview:imageView];
                    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 55, 100, 20)];
                    label.text = @"添加附件";
                    label.textAlignment = NSTextAlignmentCenter;
                    label.font = [UIFont systemFontOfSize:15];
                    [vi addSubview:label];
                } else {
                    UIButton *seebuton = [UIButton buttonWithType:UIButtonTypeSystem];
                    seebuton.frame = CGRectMake(0, 0, 100, 100);
                    [seebuton addTarget:self action:@selector(seeButtonClick:) forControlEvents:UIControlEventTouchUpInside];
                    seebuton.tag = i;
                    [vi addSubview:seebuton];
                    
                    UIImageView *xuxianImageV = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
                    xuxianImageV.image = [UIImage imageNamed:@"xuxian"];
                    [vi addSubview:xuxianImageV];
                    
                    UIImageView *deleIm = [[UIImageView alloc] initWithFrame:CGRectMake(75, 5, 20, 20)];
                    deleIm.image = [UIImage imageNamed:@"rc_dlet"];
                    [vi addSubview:deleIm];
                    
                    UIButton *buton = [UIButton buttonWithType:UIButtonTypeSystem];
                    buton.frame = CGRectMake(70, 0, 30, 30);
                    [buton addTarget:self action:@selector(deleteButonClick:) forControlEvents:UIControlEventTouchUpInside];
                    buton.tag = i;
                    [vi addSubview:buton];
                    
                    if ([type isEqualToString:@"1"]) {
                        VGReturnGoodsPicDTO *temp = [[VGReturnGoodsPicDTO alloc] init];
                        temp.picImage = [dict objectForKey:@"image"];
                        im.image = temp.picImage;
                    }else if([type isEqualToString:@"2"]){
                        [im sd_setImageWithURL:[dict objectForKey:@"image"] placeholderImage:nil];
                    }
                }
            }
        }
    }
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *indexStr = [NSString stringWithFormat:@"%ld",indexPath.section];
    if ([indexStr isEqualToString:@"0"]) {
        if (indexPath.row == 0) {
            NSArray *arr = [NSArray arrayWithObjects:@"否",@"是", nil];
            ShowMenuView *menuView = [[ShowMenuView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height) title:@"无房认筹" titleNmae:@"请选择" numLits:nil items:arr];
            menuView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
            [[UIApplication sharedApplication].keyWindow addSubview:menuView];
            __weak ShowMenuView *weakMenuView = menuView;
            [menuView setTouchBlock:^(NSString *indet, NSString *numberString) {
                if ([NSString isStringEmpty2:indet] == NO) {
                    _rcRoomName = nil;
                    _rcRoomId = nil;
                    _calculateArea = nil;
                    _discountPrice = nil;
                    _discountTotalPrice = nil;
                    if ([NSString isStringEmpty:indet] == NO) {
                        if ([indet isEqualToString:@"否"]) {
                            grouparr1 = [[NSMutableArray alloc] initWithObjects:@"无房认筹",@"认筹房源",@"计价面积",@"折前单价",@"折前总价", nil];
                            [dic setValue:grouparr1 forKey:@"0"];
                        }else {
                            grouparr1 = [[NSMutableArray alloc] initWithObjects:@"无房认筹",nil];
                            [dic setValue:grouparr1 forKey:@"0"];
                        }
                    }else {
                        grouparr1 = [[NSMutableArray alloc] initWithObjects:@"无房认筹",@"认筹房源",@"计价面积",@"折前单价",@"折前总价", nil];
                        [dic setValue:grouparr1 forKey:@"0"];
                    }
                    _isPayDeposit = indet;
                    _isPayDepositNumber = [NSString stringWithFormat:@"%ld", [numberString integerValue]-1 ];
                }
                [weakMenuView removeFromSuperview];
                [_tableView reloadData];
            }];
        }else if (indexPath.row == 1) {
            HousingViewController *hvn = [[HousingViewController alloc] init];
            [hvn setHousingNewBlock:^(NSMutableArray *arr, NSString *strName) {
                _roomList = [arr objectAtIndex:0];
                _rcRoomName = strName;
                _rcRoomId = _roomList.roomId;
                _calculateArea = _roomList.calculateArea;
                _discountPrice = _roomList.discountPrice;
                _discountTotalPrice = _roomList.discountTotalPrice;
                [_tableView reloadData];
            }];
            [self.navigationController pushViewController:hvn animated:YES];
        }
    }else {
        if (indexPath.row == 2) {
            if (_zjlxList.count == 0) {
                [self showToast:@"暂无可选项"];
                return;
            }
            NSMutableArray *arr = [[NSMutableArray alloc] init];
            for (int i = 0; i < _zjlxList.count; i++) {
                NSMutableDictionary *dict = [_zjlxList objectAtIndex:i];
                [arr addObject:[dict objectForKey:@"dictionaryName"]];
            }
           
            ShowMenuView *menuView = [[ShowMenuView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height) title:@"证件号类型" titleNmae:@"请选择" numLits:nil items:arr];
            menuView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
            [[UIApplication sharedApplication].keyWindow addSubview:menuView];
            __weak ShowMenuView *weakMenuView = menuView;
            [menuView setTouchBlock:^(NSString *indet, NSString *numberString) {
                if ([NSString isStringEmpty2:indet] == NO) {
                    _khZJHLXString = indet;
                    for (int i = 0; i < _zjlxList.count; i++) {
                        NSMutableDictionary *dict = [_zjlxList objectAtIndex:i];
                        if ([_khZJHNumber isEqualToString:[dict objectForKey:@"dictionaryName"]]) {
                            _khZJHNumber = [dict objectForKey:@"dictionaryId"];
                        }
                    }
                }
                
                [weakMenuView removeFromSuperview];
                [_tableView reloadData];
            }];
        }else if (indexPath.row == 10) {
            ShowMenuView *menuView = [[ShowMenuView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height) title:@"" titleNmae:@"请选择" numLits:nil items:_zyDataArr];
            menuView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
            [[UIApplication sharedApplication].keyWindow addSubview:menuView];
            __weak ShowMenuView *weakMenuView = menuView;
            [menuView setTouchBlock:^(NSString *indet, NSString *number) {
                if ([NSString isStringEmpty2:indet] == NO) {
                    _agentName = indet;
                    
                    if (_agentName.length > 0) {
                        for (int i = 0; i < _zyData.count; i++) {
                            ConsultantList *list = [_zyData objectAtIndex:i];
                            if ([_agentName isEqualToString:list.consultantUserName]) {
                                _agentId = list.consultantUserId;
                            }
                        }
                    }
                }
                [weakMenuView removeFromSuperview];
                [_tableView reloadData];
            }];
        }else if (indexPath.row == 11) {
            TimerShowMenuView *menuView = [[TimerShowMenuView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
            menuView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
            [[UIApplication sharedApplication].keyWindow addSubview:menuView];
            __block TimerShowMenuView *weakMenuView = menuView;
            __block NewFromViewController *blockSelf = self;
            [menuView setTimerBlock:^(NSString *indet) {
                if ([NSString isStringEmpty2:indet] == NO) {
                    _timer = indet;
                }
                [weakMenuView removeFromSuperview];
                [blockSelf.tableView reloadData];
            }];
            
        }
    }
//    [_tableView reloadData];
}

#pragma mark - 删除上传图片
- (void)deleteButonClick:(UIButton *)btn {
    NSMutableDictionary *dictPic = [_picArray objectAtIndex:btn.tag];
    NSString *type = [dictPic objectForKey:@"type"];
    if ([type isEqualToString:@"2"]) {
        NSString *url = [dictPic objectForKey:@"image"];
        NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
        [dict setObject:url forKey:@"signedUrl"];
        NSString *str = @"1";
        [dict setObject:str forKey:@"headType"];
        [_imageList addObject:dict];
    }
    [_picArray removeObjectAtIndex:btn.tag];
    [_tableView reloadData];
}

#pragma mark - 查看大图
- (void)seeButtonClick:(UIButton *)btn {
    if (_picArray.count > 0) {
        MHPhotoBrowserController *vc = [MHPhotoBrowserController new];
        NSMutableArray * bigImgArray = [NSMutableArray new];
        for (int i = 0; i < _picArray.count; i++) {
            NSMutableDictionary *dict = [_picArray objectAtIndex:i];
            NSString *type = [dict objectForKey:@"type"];
            if ([type isEqualToString:@"1"]) {
                [bigImgArray addObject:[MHPhotoModel photoWithImage:[dict objectForKey:@"image"]]];
            }else if([type isEqualToString:@"2"]){
                [bigImgArray addObject:[MHPhotoModel photoWithURL:[NSURL URLWithString:[dict objectForKey:@"image"]]]];
            }
        }
        vc.currentImgIndex = (int)btn.tag;
        vc.displayTopPage = YES;
        vc.displayDeleteBtn = YES;
        vc.imgArray = bigImgArray;
        //        vc.isShouDel = 1;
        vc.displayDeleteBtn = NO;
        [self presentViewController:vc animated:YES completion:nil];
    }else {
        [self showToast:@"暂无图片预览"];
    }
}


#pragma mark - 新增客户成员
- (void)newKHCYBtnClick:(UIButton *)btn {
    
    NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
    [dict setObject:@"" forKey:@"memberName"];
    [dict setObject:@"" forKey:@"documentType"];
    [dict setObject:@"" forKey:@"documentNumber"];
    [_khxxDataArr addObject:dict];
    [_tableView reloadData];
}

//- 认购删除家庭成员
- (void)dletjtcyBtnClick:(UIButton *)btn {
    [_khxxDataArr removeObjectAtIndex:btn.tag - 100];
    [_tableView reloadData];
}

//- 选择家庭成员证件类型
- (void)zjleiBtnClick:(UIButton *)btn {
    //    NSArray *arr = [NSArray arrayWithObjects:@"身份证",@"护照",@"营业执照",
    //                    @"户口薄",@"军官证", @"临时身份证",@"港澳台同胞来往通行证",
    //                    @"边民出入境通行证", @"外国人永久居留证",@"警官证", @"士兵证",@"其他", nil];
    if (_zjlxList.count == 0) {
        [self showToast:@"暂无可选项"];
        return;
    }
    NSMutableArray *arr = [[NSMutableArray alloc] init];
    for (int i = 0; i < _zjlxList.count; i++) {
        NSMutableDictionary *dict = [_zjlxList objectAtIndex:i];
        [arr addObject:[dict objectForKey:@"dictionaryName"]];
    }
   
    ShowMenuView *menuView = [[ShowMenuView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height) title:@"证件号类型" titleNmae:@"请选择" numLits:nil items:arr];
    menuView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
    [[UIApplication sharedApplication].keyWindow addSubview:menuView];
    __weak ShowMenuView *weakMenuView = menuView;
    [menuView setTouchBlock:^(NSString *indet, NSString *numberString) {
        NSLog(@"%@",indet);
        if ([NSString isStringEmpty2:indet] == NO) {
            NSString *documentId = nil;
            for (int i = 0; i < _zjlxList.count; i++) {
                for (int i = 0; i < _zjlxList.count; i++) {
                    NSMutableDictionary *dict = [_zjlxList objectAtIndex:i];
                    if ([indet isEqualToString:[dict objectForKey:@"dictionaryName"]]) {
                        documentId = [dict objectForKey:@"dictionaryId"];
                    }
                }
            }
            NSMutableDictionary *dict = [_khxxDataArr objectAtIndex:btn.tag - 100];
            [dict setObject:documentId forKey:@"documentType"];
        }
        [weakMenuView removeFromSuperview];
        [_tableView reloadData];
    }];
}

//输入客户姓名
- (void)khNameTextFieldClick:(UITextField *)textField {
    NSMutableDictionary *dict = [_khxxDataArr objectAtIndex:textField.tag - 100];
    if ([NSString isStringEmpty2:textField.text] == NO) {
        [dict setObject:textField.text forKey:@"memberName"];
    }else {
        [dict setObject:@"" forKey:@"memberName"];
    }
}

//输入证件号码
- (void)zjhmTextFieldFieldClick:(UITextField *)textField {
    NSMutableDictionary *dict = [_khxxDataArr objectAtIndex:textField.tag - 100];
    if ([NSString isStringEmpty2:textField.text] == NO) {
        [dict setObject:textField.text forKey:@"documentNumber"];
    }else {
        [dict setObject:@"" forKey:@"documentNumber"];
    }
}

-(void)doButton:(UIButton *)sender {
    NSString *string = [NSString stringWithFormat:@"%ld",sender.tag-100];
    //数组selectedArr里面存的数据和表头想对应，方便以后做比较
    if ([selectedArr containsObject:string]) {
        [selectedArr removeObject:string];
    }else {
        [selectedArr addObject:string];
    }
    [_tableView reloadData];
    
}

//  --- UITextField结束编辑代理方法
-(void)textFieldDidEndEditing:(UITextField *)textField {
    if (textField == _khNameTextField) {
        _khNameString = textField.text;
    }else if (textField == _khZJHTextField) {
        _khZJHString = textField.text;
    }else if (textField == _khYLHTextField) {
        _khYLHString = textField.text;
    }else if (textField == _khYJDZTextField) {
        _khYJDZString = textField.text;
    }else if (textField == _khYBTextField) {
        if (textField.text.length != 0) {
            if (textField.text.length >6) {
                [self showToast:@"邮编不超过六位"];
                [_tableView reloadData];
            }else {
                _khYBString = textField.text;
            }
        }
    }else if (textField == _khRCHTextField) {
        _khRCHString = textField.text;
    }else if (textField == _khRCJTextField) {
        _khRCJString = textField.text;
    }else if (textField == _khBZTextField) {
        _khBZString = textField.text;
    }else if (textField == _khxxTextField) {
        [_tableView reloadData];
    }else if (textField == _khxxZJHTextField) {
        [_tableView reloadData];
    }
}

- (void)documentTypeBtnClick:(UIButton *)btn {
//    NSArray *arr = [NSArray arrayWithObjects:@"身份证",@"护照",@"营业执照",
//                    @"户口薄",@"军官证", @"临时身份证",@"港澳台同胞来往通行证",
//                    @"边民出入境通行证", @"外国人永久居留证",@"警官证", @"士兵证",@"其他", nil];
    if (_zjlxList.count == 0) {
        [self showToast:@"暂无可选项"];
        return;
    }
    NSMutableArray *arr = [[NSMutableArray alloc] init];
    for (int i = 0; i < _zjlxList.count; i++) {
        NSMutableDictionary *dict = [_zjlxList objectAtIndex:i];
        [arr addObject:[dict objectForKey:@"dictionaryName"]];
    }
   
    ShowMenuView *menuView = [[ShowMenuView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height) title:@"证件号类型" titleNmae:@"请选择" numLits:nil items:arr];
    menuView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
    [[UIApplication sharedApplication].keyWindow addSubview:menuView];
    __weak ShowMenuView *weakMenuView = menuView;
    [menuView setTouchBlock:^(NSString *indet, NSString *numberString) {
        NSLog(@"%@",indet);
        if ([NSString isStringEmpty2:indet] == NO) {
            NSString *documentId = nil;
            for (int i = 0; i < _zjlxList.count; i++) {
                for (int i = 0; i < _zjlxList.count; i++) {
                    NSMutableDictionary *dict = [_zjlxList objectAtIndex:i];
                    if ([indet isEqualToString:[dict objectForKey:@"dictionaryName"]]) {
                        documentId = [dict objectForKey:@"dictionaryId"];
                    }
                }
            }
            NSMutableDictionary *dict = [_khxxDataArr objectAtIndex:btn.tag];
            [dict setObject:documentId forKey:@"documentType"];
            [_cellTableView reloadData];
        }
        [weakMenuView removeFromSuperview];
    }];
}

#pragma mark - 保存
- (void)bcBtnClick:(UIButton *)btn {
    if ([self.isFrom isEqualToString:@"0"]) {
        [self postGetCustomerListApiAuditStatus:@"0"];
    }else {
        [self postEditPayDepositApiAuditStatus:@"0"];
    }
}

#pragma mark -提交
- (void)tjBtnClick:(UIButton *)btn {
    if ([self.isFrom isEqualToString:@"0"]) {
        [self postGetCustomerListApiAuditStatus:@"1"];
    }else {
        [self postEditPayDepositApiAuditStatus:@"1"];
    }
}

#pragma mark - post 新增认筹
/**
 *  功能描述：
 *  1.判断姓名必须
 *  创建时间：16/5/11
 *
 *  更新时间：2017/02/17 下午
 *  更新人： wangguiping
 */
- (void)postGetCustomerListApiAuditStatus:(NSString *)auditStatus{
    if ([NSString isStringEmpty2:_isPayDeposit] == YES) {
        [self showToast:@"未选择是否无房认筹"];
        return;
    }
    if ([_isPayDeposit isEqualToString:@"是"]) {
    }else {
        if ([NSString isStringEmpty2:_rcRoomId] == YES) {
            [self showToast:@"未选择认筹房源"];
            return;
        }
        if ([NSString isStringEmpty2:_calculateArea] == YES) {
            [self showToast:@"计价面积未输入"];
            return;
        }
        if ([NSString isStringEmpty2:_discountPrice] == YES) {
            [self showToast:@"折前单价未输入"];
            return;
        }
        if ([NSString isStringEmpty2:_discountTotalPrice] == YES) {
            [self showToast:@"折前总价未输入"];
            return;
        }
    }
    if (_khxxDataArr.count > 0) {
        for (int i = 0; i < _khxxDataArr.count; i++) {
            NSMutableDictionary *dict = [_khxxDataArr objectAtIndex:i];
            NSString *memberName = [dict objectForKey:@"memberName"];
            NSString *documentType = [dict objectForKey:@"documentType"];
            NSString *documentNumber = [dict objectForKey:@"documentNumber"];
            if ([NSString isStringEmpty2:memberName] == YES) {
                [self showToast:@"新增客户信息名字未填写"];
                return;
            }
            if ([NSString isStringEmpty2:documentType] == YES) {
                [self showToast:@"新增客户信息证件类未选择"];
                return;
            }
            if ([NSString isStringEmpty2:documentNumber] == YES) {
                [self showToast:@"新增客户信息证件号未填写"];
                return;
            }
        }
    }
//    if ([NSString isStringEmpty2:_khRCHString] == YES) {
//        [self showToast:@"未输入认筹编号"];
//        return;
//    }
    if ([NSString isStringEmpty2:_khRCJString] == YES) {
        [self showToast:@"未输入认筹金"];
        return;
    }
    if ([NSString isStringEmpty2:_agentId] == YES) {
        [self showToast:@"经纪人不能为空"];
        return;
    }
    if ([NSString isStringEmpty2:_timer] == YES) {
        [self showToast:@"认筹时间未选择"];
        return;
    }
    if ([NSString isStringEmpty2:_khNameString] == YES) {
        [self showToast:@"客户名字未输入"];
        return;
    }
//
//    if ([NSString isStringEmpty2:_khZJHNumber] == YES) {
//        [self showToast:@"证件类型未选择"];
//        return;
//    }
//    
//    if ([NSString isStringEmpty2:_khZJHString] == YES) {
//        [self showToast:@"证件号码未输入"];
//        return;
//    }
//    
//    if ([NSString isStringEmpty2:_khYLHString] == YES) {
//        [self showToast:@"手机号码未输入"];
//        return;
//    }
//    
//    if ([NSString isStringEmpty2:_khYJDZString] == YES) {
//        [self showToast:@"邮寄地址未输入"];
//        return;
//    }
//    
//    if ([NSString isStringEmpty2:_khYBString] == YES) {
//        [self showToast:@"邮编未输入"];
//        return;
//    }
//    if (_khYBString.length != 0) {
//        if (_khYBString.length >6) {
//            [self showToast:@"邮编不超过六位"];
//            return;
//        }
//        
//    }
//    if ([NSString isStringEmpty2:_khBZString] == YES) {
//        [self showToast:@"备注未输入"];
//        return;
//    }
    
//    [self showHud:@"正在请求稍等......"];
    [self showHudInWindow:@"正在请求稍等......"];
    
    NSMutableArray *arr = [[NSMutableArray alloc] init];
    
    if (_imageList.count > 0) {
        for (int j = 0; j < _imageList.count; j++) {
            NSMutableDictionary *dictImage = [_imageList objectAtIndex:j];
            NSString *headType = [dictImage objectForKey:@"headType"];
            if ([headType isEqualToString:@"1"] ) {
                [arr addObject:dictImage];
            }
        }
    }
    
    [_imageList removeAllObjects];
    
    for (int j = 0; j < arr.count; j++) {
        NSMutableDictionary *dictImage = [arr objectAtIndex:j];
        [_imageList addObject:dictImage];
    }
    
    
    for (int i=0; i<_picArray.count; i++) {
        NSMutableDictionary *dictPic = [_picArray objectAtIndex:i];
        NSString *type = [dictPic objectForKey:@"type"];
        if ([type isEqualToString:@"1"]) {
            
            NSData* imageData = UIImageJPEGRepresentation([dictPic objectForKey:@"image"], 0.1);
            NSString *base64EncodedString = [NSString base64StringFromData:imageData length:[imageData length]];
            NSString *encodedImageStr = base64EncodedString;
            NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
            [dict setObject:encodedImageStr forKey:@"signedUrl"];
            [dict setObject:@".png" forKey:@"headType"];
            [_imageList addObject:dict];
        }else if ([type isEqualToString:@"2"]){
            NSString *url = [dictPic objectForKey:@"image"];
            NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
            [dict setObject:url forKey:@"signedUrl"];
            NSString *str = @"0";
            [dict setObject:str forKey:@"headType"];
            [_imageList addObject:dict];
        }
    }
    
    AddPayDepositApi *api = [[AddPayDepositApi alloc] initWithCustomerId:self.customerId
                                                            isPayDeposit:_isPayDepositNumber
                                                                  roomId:_rcRoomId
                                                           calculateArea:_calculateArea
                                                           discountPrice:_discountPrice
                                                      discountTotalPrice:_discountTotalPrice
                                                          payDepositCode:_khRCHString
                                                         payDepositMoney:_khRCJString
                                                                 agentId:_agentId
                                                          payDepositDate:_timer
                                                                  remark:_khBZString
                                                             auditStatus:auditStatus
                                                              memberName:_khNameString
                                                            documentType:_khZJHNumber
                                                          documentNumber:_khZJHString
                                                                  mobile:_khYLHString
                                                                mailPost:_khYJDZString
                                                                postcode:_khYBString
                                                                    type:@"2"
                                                                category:@"1"
                                                      customerFamilyList:_khxxDataArr
                                                               imageList:_imageList];
    [api startWithCompletionBlockWithSuccess:^(YTKBaseRequest *request) {
        AddPayDepositApi *result = (AddPayDepositApi *)request;
        if (result.isSuccess) {
            [self hideHud];
            if ([result.resultData.result isEqualToString:@"1"]) {
                if ([NSString isStringEmpty2:result.resultData.errorMsg] == NO) {
                    [self showToast:result.resultData.errorMsg];
                }else {
                    [self.navigationController popViewControllerAnimated:YES];
                }
            }else {
                [self.navigationController popViewControllerAnimated:YES];
            }
        } else {
//            [self showToast:result.resultData.errorMsg];
            NSUserDefaults *userdf = [NSUserDefaults standardUserDefaults];
            NSString *projectCode = [userdf objectForKey:@"projectCode"];
            if ([NSString isStringEmpty2:projectCode] == NO) {
                [self showToast:result.resultData.errorMsg];
            }
            
            /*
             提示语：账号权限发生变动，请重新登录
             code随机18位唯一码：108E65843mYuwqb01X
             //YUplf0wyU5vPVssLSG 对应的异地登录退出登录
             */
            if ([result.resultData.errorCode isEqualToString:@"YUplf0wyU5vPVssLSG"]) {
                NSUserDefaults *defs = [NSUserDefaults standardUserDefaults];
                NSDictionary *dictionary = [defs dictionaryRepresentation];
                for(NSString *key in [dictionary allKeys]){
                    if ([key isEqualToString:@"DeviceToken123"] ||
                        [key isEqualToString:@"loginText"] ||
                        [key isEqualToString:@"provinceCityDistrict"]) {
                    }else {
                        [defs removeObjectForKey:key];
                        [defs synchronize];
                    }
                }
                [appInstance showLogin];
                
            } else if ( [result.resultData.errorCode isEqualToString:@"108E65843mYuwqb01X"]) {
                NSUserDefaults *defs = [NSUserDefaults standardUserDefaults];
                NSDictionary *dictionary = [defs dictionaryRepresentation];
                for(NSString *key in [dictionary allKeys]){
                    if ([key isEqualToString:@"DeviceToken123"] ||
                        [key isEqualToString:@"loginText"] ||
                        [key isEqualToString:@"provinceCityDistrict"]) {
                    }else {
                        [defs removeObjectForKey:key];
                        [defs synchronize];
                    }
                }
                UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"温馨提示" message:result.resultData.errorMsg preferredStyle:UIAlertControllerStyleAlert];
                UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
                    [appInstance showLogin];
                }];
                [alertController addAction:cancelAction];
                [self presentViewController:alertController animated:YES completion:nil];
            }
        }
        [self hideHud];
    } failure:^(YTKBaseRequest *request) {
        [self showToast:@"连接服务器失败，请检查网络！"];
        [self hideHud];
    }];
}

#pragma mark - post 修改认筹
- (void)postEditPayDepositApiAuditStatus:(NSString *)auditStatus{
    //    [self showHud:@"正在请求稍等......"];
    if ([NSString isStringEmpty2:_isPayDepositNumber] == YES) {
        [self showToast:@"未选择是否无房认筹"];
        return;
    }
    if ([_isPayDeposit isEqualToString:@"是"]) {
        
    }else {
        if ([NSString isStringEmpty2:_rcRoomId] == YES) {
            [self showToast:@"未选择认筹房源"];
            return;
        }
        
        if ([NSString isStringEmpty2:_calculateArea] == YES) {
            [self showToast:@"计价面积未输入"];
            return;
        }
        if ([NSString isStringEmpty2:_discountPrice] == YES) {
            [self showToast:@"折前单价未输入"];
            return;
        }
        if ([NSString isStringEmpty2:_discountTotalPrice] == YES) {
            [self showToast:@"折前总价未输入"];
            return;
        }
    }
//    if ([NSString isStringEmpty2:_khRCHString] == YES) {
//        [self showToast:@"未输入认筹编号"];
//        return;
//    }
    if (_khxxDataArr.count > 0) {
        for (int i = 0; i < _khxxDataArr.count; i++) {
            NSMutableDictionary *dict = [_khxxDataArr objectAtIndex:i];
            NSString *memberName = [dict objectForKey:@"memberName"];
            NSString *documentType = [dict objectForKey:@"documentType"];
            NSString *documentNumber = [dict objectForKey:@"documentNumber"];
            if ([NSString isStringEmpty2:memberName] == YES) {
                [self showToast:@"新增客户信息名字未填写"];
                return;
            }
            if ([NSString isStringEmpty2:documentType] == YES) {
                [self showToast:@"新增客户信息证件类未选择"];
                return;
            }
            if ([NSString isStringEmpty2:documentNumber] == YES) {
                [self showToast:@"新增客户信息证件号未填写"];
                return;
            }
        }
    }
    if ([NSString isStringEmpty2:_khRCJString] == YES) {
        [self showToast:@"未输入认筹金"];
        return;
    }
    if ([NSString isStringEmpty2:_agentId] == YES) {
        [self showToast:@"经纪人不能为空"];
        return;
    }
    if ([NSString isStringEmpty2:_timer] == YES) {
        [self showToast:@"认筹时间未选择"];
        return;
    }
//    if ([NSString isStringEmpty2:_khNameString] == YES) {
//        [self showToast:@"客户名字未输入"];
//        return;
//    }
//    
//    if ([NSString isStringEmpty2:_khZJHNumber] == YES) {
//        [self showToast:@"证件类型未选择"];
//        return;
//    }
//    
//    if ([NSString isStringEmpty2:_khZJHString] == YES) {
//        [self showToast:@"证件号码未输入"];
//        return;
//    }
//    
//    if ([NSString isStringEmpty2:_khYLHString] == YES) {
//        [self showToast:@"手机号码未输入"];
//        return;
//    }
//    
//    if ([NSString isStringEmpty2:_khYJDZString] == YES) {
//        [self showToast:@"邮寄地址未输入"];
//        return;
//    }
//    
//    if ([NSString isStringEmpty2:_khYBString] == YES) {
//        [self showToast:@"邮编未输入"];
//        return;
//    }
//    if (_khYBString.length != 0) {
//        if (_khYBString.length >6) {
//            [self showToast:@"邮编不超过六位"];
//            return;
//        }
//        
//    }
//    if ([NSString isStringEmpty2:_khBZString] == YES) {
//        [self showToast:@"备注未输入"];
//        return;
//    }
    [self showHudInWindow:@"正在请求稍等......"];
    
    NSMutableArray *arr = [[NSMutableArray alloc] init];
    
    if (_imageList.count > 0) {
        for (int j = 0; j < _imageList.count; j++) {
            NSMutableDictionary *dictImage = [_imageList objectAtIndex:j];
            NSString *headType = [dictImage objectForKey:@"headType"];
            if ([headType isEqualToString:@"1"] ) {
                [arr addObject:dictImage];
            }
        }
    }
    
    [_imageList removeAllObjects];
    
    for (int j = 0; j < arr.count; j++) {
        NSMutableDictionary *dictImage = [arr objectAtIndex:j];
        [_imageList addObject:dictImage];
    }
    
    for (int i=0; i<_picArray.count; i++) {
        NSMutableDictionary *dictPic = [_picArray objectAtIndex:i];
        NSString *type = [dictPic objectForKey:@"type"];
        if ([type isEqualToString:@"1"]) {
            NSData* imageData = UIImageJPEGRepresentation([dictPic objectForKey:@"image"], 0.1);
            NSString *base64EncodedString = [NSString base64StringFromData:imageData length:[imageData length]];
            NSString *encodedImageStr = base64EncodedString;
            NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
            [dict setObject:encodedImageStr forKey:@"signedUrl"];
            [dict setObject:@".png" forKey:@"headType"];
            [_imageList addObject:dict];
        }else if ([type isEqualToString:@"2"]){
            NSString *url = [dictPic objectForKey:@"image"];
            NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
            [dict setObject:url forKey:@"signedUrl"];
            NSString *str = @"0";
            [dict setObject:str forKey:@"headType"];
            [_imageList addObject:dict];
        }
    }
    
    EditPayDepositApi *api = [[EditPayDepositApi alloc] initWithPayDepositId:_getPayDepositDetail.payDepositId
                                                                isPayDeposit:_isPayDepositNumber
                                                                      roomId:_rcRoomId
                                                               calculateArea:_calculateArea
                                                               discountPrice:_discountPrice
                                                          discountTotalPrice:_discountTotalPrice
                                                              payDepositCode:_khRCHString
                                                             payDepositMoney:_khRCJString
                                                                     agentId:_agentId
                                                              payDepositDate:_timer
                                                                      remark:_khBZString
                                                                 auditStatus:auditStatus
                                                                  memberName:_khNameString
                                                                documentType:_khZJHNumber
                                                              documentNumber:_khZJHString
                                                                      mobile:_khYLHString
                                                                    mailPost:_khYJDZString
                                                                    postcode:_khYBString
                                                          customerFamilyList:_khxxDataArr
                                                                   imageList:_imageList];
    
    [api startWithCompletionBlockWithSuccess:^(YTKBaseRequest *request) {
        EditPayDepositApi *result = (EditPayDepositApi *)request;
        if (result.isSuccess) {
            [self hideHud];
            if ([result.resultData.result isEqualToString:@"1"]) {
                if ([NSString isStringEmpty2:result.resultData.errorMsg] == NO) {
                    [self showToast:result.resultData.errorMsg];
                }else {
                    if (_followUpViewBlock) {
                        _followUpViewBlock();
                        [self.navigationController popViewControllerAnimated:YES];
                    }
                }
            }else {
                if (_followUpViewBlock) {
                    _followUpViewBlock();
                    [self.navigationController popViewControllerAnimated:YES];
                }
            }
            
        } else {
//            [self showToast:result.resultData.errorMsg];
            NSUserDefaults *userdf = [NSUserDefaults standardUserDefaults];
            NSString *projectCode = [userdf objectForKey:@"projectCode"];
            if ([NSString isStringEmpty2:projectCode] == NO) {
                [self showToast:result.resultData.errorMsg];
            }
            
            /*
             提示语：账号权限发生变动，请重新登录
             code随机18位唯一码：108E65843mYuwqb01X
             //YUplf0wyU5vPVssLSG 对应的异地登录退出登录
             */
            if ([result.resultData.errorCode isEqualToString:@"YUplf0wyU5vPVssLSG"]) {
                NSUserDefaults *defs = [NSUserDefaults standardUserDefaults];
                NSDictionary *dictionary = [defs dictionaryRepresentation];
                for(NSString *key in [dictionary allKeys]){
                    if ([key isEqualToString:@"DeviceToken123"] ||
                        [key isEqualToString:@"loginText"] ||
                        [key isEqualToString:@"provinceCityDistrict"]) {
                    }else {
                        [defs removeObjectForKey:key];
                        [defs synchronize];
                    }
                }
                [appInstance showLogin];
                
            } else if ( [result.resultData.errorCode isEqualToString:@"108E65843mYuwqb01X"]) {
                NSUserDefaults *defs = [NSUserDefaults standardUserDefaults];
                NSDictionary *dictionary = [defs dictionaryRepresentation];
                for(NSString *key in [dictionary allKeys]){
                    if ([key isEqualToString:@"DeviceToken123"] ||
                        [key isEqualToString:@"loginText"] ||
                        [key isEqualToString:@"provinceCityDistrict"]) {
                    }else {
                        [defs removeObjectForKey:key];
                        [defs synchronize];
                    }
                }
                UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"温馨提示" message:result.resultData.errorMsg preferredStyle:UIAlertControllerStyleAlert];
                UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
                    [appInstance showLogin];
                }];
                [alertController addAction:cancelAction];
                [self presentViewController:alertController animated:YES completion:nil];
            }
        }
        [self hideHud];
    } failure:^(YTKBaseRequest *request) {
        [self showToast:@"连接服务器失败，请检查网络！"];
        [self hideHud];
    }];
}

#pragma  mark - 证件号类型
- (void)postGetDictionaryListByProjectIdApi {
    
    GetDictionaryListByProjectIdApi *api = [[GetDictionaryListByProjectIdApi alloc] initWithBusNum:@"01"];
    [api startWithCompletionBlockWithSuccess:^(YTKBaseRequest *request) {
        GetDictionaryListByProjectIdApi *result = (GetDictionaryListByProjectIdApi *)request;
        if (result.isSuccess) {
            GetDictionaryListByProjectId *get = result.resultData;
            //12、证件类型
            _zjlxList = [get.typeMap objectForKey:@"documentType"];
            if ([self.isFrom isEqualToString:@"0"]) {
                [self postGetCustomerDetailApi];
            }
        } else {
            [self showToast:result.resultData.errorMsg];
            if ([result.resultData.errorCode isEqualToString:@"YUplf0wyU5vPVssLSG"] || [result.resultData.errorCode isEqualToString:@"108E65843mYuwqb01X"]) {
                NSUserDefaults *defs = [NSUserDefaults standardUserDefaults];
                NSDictionary *dictionary = [defs dictionaryRepresentation];
                for(NSString *key in [dictionary allKeys]){
                    if ([key isEqualToString:@"DeviceToken123"] ||
                        [key isEqualToString:@"loginText"] ||
                        [key isEqualToString:@"provinceCityDistrict"]) {
                    }else {
                        [defs removeObjectForKey:key];
                        [defs synchronize];
                    }
                }
                UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"温馨提示" message:result.resultData.errorMsg preferredStyle:UIAlertControllerStyleAlert];
                UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
                    [appInstance showLogin];
                }];
                [alertController addAction:cancelAction];
                [self presentViewController:alertController animated:YES completion:nil];
            }
        }
        [self creatNavigationController];
        [self initDataSource];
        [self hideHud];
        
    } failure:^(YTKBaseRequest *request) {
        [self showToast:@"连接服务器失败，请检查网络！"];
        [self hideHud];
    }];
}


#pragma mark - 邮寄地址和备注textview代理方法
- (void)textViewDidBeginEditing:(UITextView *)textView{
    if (textView == _mailPostTV) {//邮寄地址
        if ([textView.text isEqualToString:@"请输入邮寄地址"]) {
            textView.text = nil;
            _mailPostTV.textColor = [UIColor colorFromHexCode:@"#1A1A1A"];
        }else{
            _mailPostTV.textColor = [UIColor colorFromHexCode:@"#1A1A1A"];
        }
    }
    if (textView == _remarkTV) {//备注
        if ([textView.text isEqualToString:@"备注"]) {
            textView.text = nil;
            _remarkTV.textColor = [UIColor colorFromHexCode:@"#1A1A1A"];
        }else{
            _remarkTV.textColor = [UIColor colorFromHexCode:@"#1A1A1A"];
        }
    }
}

- (void)textViewDidChange:(UITextView *)textView{
    if (textView == _mailPostTV) {//邮寄地址
        _khYJDZString = textView.text;
        _mailPostTVHeight = [self heightForString:_mailPostTV andWidth:EMM_WIDTH - 100];
    }
    if (textView == _remarkTV) {//备注
        _khBZString = textView.text;
        _remarkTVHeight = [self heightForString:_remarkTV andWidth:EMM_WIDTH - 100];
    }
}

- (void)textViewDidEndEditing:(UITextView *)textView{
    [_tableView reloadData];
}

//计算textView的高度
- (float) heightForString:(UITextView *)textView andWidth:(float)width{
    CGSize sizeToFit = [textView sizeThatFits:CGSizeMake(width, MAXFLOAT)];
    return sizeToFit.height;
}

#pragma mark - 选择图片
- (void)butonClick:(UIButton *)btn {
    UIActionSheet *choiceSheet = [[UIActionSheet alloc] initWithTitle:nil
                                                             delegate:self
                                                    cancelButtonTitle:@"取消"
                                               destructiveButtonTitle:nil
                                                    otherButtonTitles:@"拍照", @"从相册中选取", nil];
    [choiceSheet showInView:self.view];
    
}


#pragma mark UIActionSheetDelegate
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (buttonIndex == 0) {
        // 拍照
        [self showPhotosFromCamera];
        
    } else if (buttonIndex == 1) {
        // 从相册中选取
        [self showSelectPhotos];
        
    }
}


////////////////////////////////////////////////////////////////////////////////////////////////////
#pragma mark - 调用系统相机和相册
////////////////////////////////////////////////////////////////////////////////////////////////////

#pragma mark camera utility

////////////////////////////////////////////////////////////////////////////////////////////////////

- (BOOL) isCameraAvailable
{
    return [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera];
}

- (BOOL) isRearCameraAvailable
{
    return [UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceRear];
}

- (BOOL) isFrontCameraAvailable
{
    return [UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceFront];
}

- (BOOL) doesCameraSupportTakingPhotos
{
    return [self cameraSupportsMedia:(__bridge NSString *)kUTTypeImage sourceType:UIImagePickerControllerSourceTypeCamera];
}

- (BOOL) isPhotoLibraryAvailable
{
    return [UIImagePickerController isSourceTypeAvailable:
            UIImagePickerControllerSourceTypePhotoLibrary];
}
- (BOOL) canUserPickVideosFromPhotoLibrary
{
    return [self
            cameraSupportsMedia:(__bridge NSString *)kUTTypeMovie sourceType:UIImagePickerControllerSourceTypePhotoLibrary];
}
- (BOOL) canUserPickPhotosFromPhotoLibrary
{
    return [self
            cameraSupportsMedia:(__bridge NSString *)kUTTypeImage sourceType:UIImagePickerControllerSourceTypePhotoLibrary];
}


- (BOOL) cameraSupportsMedia:(NSString *)paramMediaType sourceType:(UIImagePickerControllerSourceType)paramSourceType
{
    __block BOOL result = NO;
    if ([paramMediaType length] == 0) {
        return NO;
    }
    NSArray *availableMediaTypes = [UIImagePickerController availableMediaTypesForSourceType:paramSourceType];
    [availableMediaTypes enumerateObjectsUsingBlock: ^(id obj, NSUInteger idx, BOOL *stop) {
        NSString *mediaType = (NSString *)obj;
        if ([mediaType isEqualToString:paramMediaType]){
            result = YES;
            *stop= YES;
        }
    }];
    return result;
}
#pragma mark 1 拍照
- (void)showPhotosFromCamera
{
    
    // 拍照
    if ([self isCameraAvailable] && [self doesCameraSupportTakingPhotos]) {
        UIImagePickerController *controller = [[UIImagePickerController alloc] init];
        controller.sourceType = UIImagePickerControllerSourceTypeCamera;
        if ([self isFrontCameraAvailable]) {
            controller.cameraDevice = UIImagePickerControllerCameraDeviceRear;
        }
        
        NSMutableArray *mediaTypes = [[NSMutableArray alloc] init];
        [mediaTypes addObject:(__bridge NSString *)kUTTypeImage];
        controller.mediaTypes = mediaTypes;
        controller.delegate = self;
        [self presentViewController:controller
                           animated:YES
                         completion:^(void){
                             NSLog(@"Picker View Controller is presented");
                         }];
    } else {
        [self alertWithTitle:@"无法访问相机" msg:@"在\"设置>隐私>相机>Vgo\"中点击启用."];
    }
}

#pragma mark - UIImagePickerControllerDelegate
#pragma mark 2 拍照
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    [self showThisImagePicker:picker info:info];
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    [picker dismissViewControllerAnimated:YES completion:^(){
    }];
}


#pragma mark 3 拍照
- (void)showThisImagePicker:(UIImagePickerController *)picker info:(NSDictionary *)info
{
    [picker dismissViewControllerAnimated:YES completion:^() {
        UIImage *portraitImg = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
        NSData* imageData = UIImageJPEGRepresentation(portraitImg, 0.1);
        
        if (imageData == NULL) {
            return;
        }
        
        for (int i = 0; i < _picArray.count; i++) {
            NSMutableDictionary *jiaDict = [_picArray objectAtIndex:i];
            NSString *type = [jiaDict objectForKey:@"type"];
            if ([type isEqualToString:@"type"]) {
                [_picArray removeObjectAtIndex:i];
            }
        }
        
        NSMutableDictionary *dictIm = [[NSMutableDictionary alloc] init];
        [dictIm setObject:portraitImg forKey:@"image"];
        [dictIm setObject:@"1" forKey:@"type"];
        [_picArray addObject:dictIm];
        
        NSMutableDictionary *jDict = [[NSMutableDictionary alloc] init];
        [jDict setObject:@"image" forKey:@"image"];
        [jDict setObject:@"type" forKey:@"type"];
        [_picArray addObject:jDict];
        [_tableView reloadData];
    }];
}

#pragma mark 照片
- (void)showSelectPhotos
{
    if (![self isPhotoLibraryAvailable]) {
        [self alertWithTitle:@"无法访问相机" msg:@"在\"设置>隐私>照片>Vgo\"中点击启用."];
        return;
    }
    QBImagePickerController *imagePickerController = [QBImagePickerController new];
    imagePickerController.delegate = self;
    imagePickerController.allowsMultipleSelection = YES;
    imagePickerController.showsNumberOfSelectedAssets = YES;
    //    imagePickerController.maximumNumberOfSelection = 5;
    
    [self presentViewController:imagePickerController animated:YES completion:NULL];
}


#pragma mark - QBImagePickerControllerDelegate

- (void)qb_imagePickerController:(QBImagePickerController *)imagePickerController didSelectAssets:(NSArray *)assets
{
    NSLog(@"Selected assets:");
    NSLog(@"%@", assets);// addbtn100
    for (ALAsset *result in assets) {
        // ALAsset的类型
        NSString *assetType = [result valueForProperty:ALAssetPropertyType];
        if ([assetType isEqualToString:ALAssetTypePhoto]){
            ALAssetRepresentation *assetRepresentation =[result defaultRepresentation];
            //            CGFloat imageScale = [assetRepresentation scale];
            UIImageOrientation imageOrientation = (UIImageOrientation)[assetRepresentation orientation];
            
            CGImageRef imageReference = [assetRepresentation fullResolutionImage];
            // 对找到的图片进行操作
            UIImage *image = [[UIImage alloc] initWithCGImage:imageReference scale:1.0 orientation:imageOrientation];
            if (image != nil){
                for (int i = 0; i < _picArray.count; i++) {
                    NSMutableDictionary *jiaDict = [_picArray objectAtIndex:i];
                    NSString *type = [jiaDict objectForKey:@"type"];
                    if ([type isEqualToString:@"type"]) {
                        [_picArray removeObjectAtIndex:i];
                    }
                }
                
                
                NSMutableDictionary *dictIm = [[NSMutableDictionary alloc] init];
                [dictIm setObject:image forKey:@"image"];
                [dictIm setObject:@"1" forKey:@"type"];
                [_picArray addObject:dictIm];
                
                NSMutableDictionary *jDict = [[NSMutableDictionary alloc] init];
                [jDict setObject:@"image" forKey:@"image"];
                [jDict setObject:@"type" forKey:@"type"];
                [_picArray addObject:jDict];
                
                [_tableView reloadData];
            } else {
                NSLog(@"Failed to create the image.");
            }
        }
    }
    
    [self dismissViewControllerAnimated:YES completion:NULL];
    
}

- (void)qb_imagePickerControllerDidCancel:(QBImagePickerController *)imagePickerController
{
    [self dismissViewControllerAnimated:YES completion:NULL];
}

- (NSString *)postImage:(UIImage *)portraitImg
{
    //    portraitImg = [portraitImg imageByScalingAndCroppingForSize:CGSizeMake(60, 60)];
    
    NSData* imageData = UIImageJPEGRepresentation(portraitImg, 0.1);
    
    if (imageData == NULL) {
        return nil;
    }
    
    NSString *base64EncodedString = [NSString base64StringFromData:imageData length:[imageData length]];
    
    NSString *encodedImageStr = base64EncodedString;
    
    return encodedImageStr;
}

@end
